package org.pjlib.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.meiyou.jet.process.Jet;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.pjlib.Event;
import org.pjlib.Logs;
import org.pjlib.common.UMengUtils;
import org.pjlib.net.HttpUtils;
import org.pjlib.net.JsonCallBack;
import org.pjlib.net.MyException;
import org.pjlib.net.RequestCall;

/**
 * Created by ws on 2019/02/21.
 */
public class BaseFragment extends Fragment implements UIBase, JsonCallBack {
    protected View rootView;
    protected Activity act;
    private boolean isCreateed;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Logs.i("Base", this.getClass().getSimpleName() + ":onCreate");
        act = getActivity();
        EventBus.getDefault().register(this);
        if(getArguments()!=null){
            Jet.bind(this,getArguments());
        }
    }

    @Override
    public Object getNetTag() {
        return this;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (getContentId() > 0) {
            rootView = inflater.inflate(getContentId(), null);
        } else {
            rootView = super.onCreateView(inflater, container, savedInstanceState);
        }
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if(rootView!=null){
            Jet.bind(this,rootView);
        }
        if(!isCreateed){
            init();
            isCreateed=true;
        }
    }

    protected final <T extends View> T findViewById(@IdRes int id) {
        if (rootView == null) {
            return null;
        } else {
            return rootView.findViewById(id);
        }
    }

    @Override
    public void onBeforeRequest(RequestCall call) {

    }

    @Override
    public void onResponseError(Throwable t, RequestCall call) {
        ToastUtils.showShort(MyException.NO_AVAILABLE_NETWORK);
    }

    @Override
    public void onResponseSuccess(RequestCall call) {

    }

    @Override
    public void onFinishRequest(RequestCall call) {

    }

    @Override
    public int getContentId() {
        return -1;
    }

    @Override
    public void init() {

    }

    @Override
    public void updata() {

    }

    @Override
    public void toActivity(Class<? extends BaseActivity> c) {
        ActivityUtils.startActivity(c);
    }

    @Override
    public void toActivity(Class<? extends BaseActivity> c, Bundle bundle) {
        Intent intent = new Intent(act, c);
        intent.putExtras(bundle);
        act.startActivity(intent);
    }

    @Override
    public void toActivity(Intent intent) {
        act.startActivity(intent);
    }

    @Subscribe
    public void onEvent(Event.Updata event) {
        updata();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Logs.i("BaseFragment", this.getClass().getSimpleName() + ":onDestroy");
        EventBus.getDefault().unregister(this);
        HttpUtils.cancelTag(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        UMengUtils.onPageStart(getClass().getSimpleName());
    }

    @Override
    public void onPause() {
        super.onPause();
        UMengUtils.onPageEnd(getClass().getSimpleName());
    }

}
